var vm = new Vue({
    el:"#app",
    data () {
        return {
            formItem: {
                InNo:'',
                Type:'',
                ProductNumber:1,
                WarehouseNo:'',
                FromStore:''
            },
            queryForm:{
                DealerNo:'',
                ProductName:'',
                WarehouseNo:'',
                Type:'',
                InNo:''
            },
            modal1: false, //入库弹框
            level1:true,
            plan:false,
            //仓库
            wareHouses:[],
            //经销商
            Dealers:[],
            //店面
            Store:'',
            //是不是财务部门
            isBm:false,
            //操作人员
            CreateUser:'',
            //当天日期
            NowDate:'',
            //选中产品数量
            ProductNumber:'',
            //入库数量是否禁用
            numberDis:false,
            //选择入库类型
            inType:'',
            //剩余数量
            RestNumber:'',
            //搜索结果
            columns1: [
                {
                    type: 'index',
                    title: '序号',
                    width:46,
                    align: 'center',
                },
                {
                    title: '入库编号',
                    key: 'InNo',
                    align: 'center',
                },
                {
                    title: '入库类型',
                    key: 'Type',
                    align: 'center',
                    render:(h, params)=>{
                        var isShow='';
                        if (params.row.Type==1){
                            isShow = "调拨入库";
                        }
                        if (params.row.Type==0){
                            isShow = "采购入库";
                        }
                        return h('span', isShow)
                    }
                },
                {
                    title: '店面',
                    key: 'Store',
                    align: 'center',
                },
                {
                    title: '经销商名称',
                    key: 'dalerName',
                    align: 'center',
                    render:(h,params) => {
                        if(params.row.dalerName==''||params.row.dalerName==null){
                            return h("span","-");
                        }else{
                            return h("span",params.row.dalerName);
                        }
                    }
                },
                {
                    title: '调拨店面',
                    key: 'FromStore',
                    align: 'center',
                    render:(h,params) => {
                        if(params.row.FromStore==''||params.row.FromStore==null){
                            return h("span","-");
                        }else{
                            return h("span",params.row.FromStore);
                        }
                    }
                },
                {
                    title: '产品名称',
                    key: 'ProductName',
                    align: 'center',
                },
                {
                    title: '数量',
                    key: 'ProductNumber',
                    align: 'center',
                    width:100,
                },
                {
                    title: '单价',
                    key: 'ProductPrice',
                    align: 'center',
                    width:100,
                },
                {
                    title: '仓库',
                    key: 'warehouseName',
                    align: 'center',
                },
                {
                    title: '入库时间',
                    key: 'CreateTime',
                    align: 'center',
                },
                {   title: '操作',
                    key: 'action',
                    align: 'center',
                    width:150,
                    render: (h, params) => {
                        return h('div', [
                            /*h('a', {
                                style: {
                                    marginRight: '5px',
                                    border : '0',
                                    fontSize: '14px',
                                },
                                on: {
                                    click: () => {
                                        this.edit(params.row);
                                    }
                                }
                            },'编辑'),
                            h('span', {
                            },'|'),*/
                            h('a', {
                                style: {
                                    marginLeft: '5px',
                                    border : '0',
                                    fontSize: '14px',
                                },
                                on:{
                                    click:()=>{
                                        this.deleteIn(params.row.InNo,params.row.ProductNumber,params.row.CreateUser,params.row.CreateTime,params.row.PlanNo,params.row.Type);
                                    }
                                }
                            },'删除')
                        ])
                    }
                }
            ],
            rkTotal:0,
            rkCurrent:1,
            rkPageSize:15,
            data1: [ ],
            //选择采购计划弹框
            columns3: [],
            data3: [],
            c1Total:0,
            c1Current:1,
            c1PageSize:15,

        }
    },
    methods: {
        ok () {
        },
        cancel () {
        },
        // 编辑弹框
        edit(parm){
            this.formItem.InNo=parm.InNo;
            this.formItem.Type=parm.Type;
            this.formItem.ProductName=parm.ProductName;
            this.formItem.ProductNumber=parm.ProductNumber;
            this.formItem.WarehouseNo=parm.WarehouseNo;
            this.formItem.DealerNo=parm.DealerNo;
            this.formItem.ProductNo=parm.ProductNo;
            this.formItem.ProductPrice=parm.ProductPrice;
            this.formItem.PlanNo=parm.PlanNo;
            this.modal1=true;
        },
        //搜索
        rkbeforequerytable(current){
            var self = this;
            self.rkCurrent = current;
            self.queryIn();
        },
        queryIn(){
            var vueThis = this;
            this.queryForm.Store=this.Store;
            var rkTotal = 0;
            var rkCurrent = vueThis.rkCurrent;
            var rkPageSize = vueThis.rkPageSize;
            var jsondata = this.queryForm;
            jsondata.rkCurrent = rkCurrent;
            jsondata.rkPageSize = rkPageSize;
            var data_json = JSON.stringify(jsondata);

            $.ajax({
                url: "./goInBound/queryAllInBound", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    vueThis.data1 = data.ResultList;
                    rkTotal = data.rkTotal;
                    vueThis.rkTotal = rkTotal;
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        //获取单据号
        uuid(len, radix) {
            var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
            var uuid = [], i;
            radix = radix || chars.length;

            if (len) {
                // Compact form
                for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
            } else {
                // rfc4122, version 4 form
                var r;

                // rfc4122 requires these characters
                uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
                uuid[14] = '4';

                // Fill in random data. At i==19 set the high bits of clock sequence as
                // per rfc4122, sec. 4.1.5
                for (i = 0; i < 36; i++) {
                    if (!uuid[i]) {
                        r = 0 | Math.random()*16;
                        uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
                    }
                }
            }

            return uuid.join('');
        },
        //点击入库
        clickIn(){
            this.initAlert();
            this.modal1 = true;
            this.formItem.InNo = "RK-"+this.uuid(8, 16);
        },
        //获取仓库
        getWh(){
            var jsondata = {}
            var data_json = JSON.stringify(jsondata);
            var vueThis = this;
            $.ajax({
                url: "./goWh/queryWhByParm", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    vueThis.wareHouses=data.ResultList;
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        //获取经销商
        getDealer(){
            var jsondata = {}
            var data_json = JSON.stringify(jsondata);
            var vueThis = this;
            $.ajax({
                url: "./goJxs/queryAllJxs", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    vueThis.Dealers=data.ResultList;
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        beforequerytable(current){
            var self = this;
            self.c1Current = current;
            self.getInType();
        },
        //获取入库类型
        getInType(value){
            var vueThis = this;
            this.inType=this.formItem.Type;
            value = this.formItem.Type;
            if (Number(value)==0){
                this.columns3= [
                    {
                        type: 'index',
                        title: '序号',
                        width:46,
                        align: 'center',
                    },
                    {
                        title: '计划编号',
                        key: 'PlanNo',
                        align: 'center',
                    },
                    {
                        title: '经销商名称',
                        key: 'DealerName',
                        align: 'center',
                    },
                    {
                        title: '产品编号',
                        key: 'ProductNo',
                        align: 'center',
                    },
                    {
                        title: '产品名称',
                        key: 'ProductName',
                        align: 'center',
                    },
                    {
                        title: '产品规格',
                        key: 'Specification',
                        align: 'center',
                    },
                    {
                        title: '产品型号',
                        key: 'Type',
                        align: 'center',
                    },
                    {
                        title: '产品单价',
                        key: 'ProductPrice',
                        align: 'center',
                    },
                    {
                        title: '产品数量',
                        key: 'ProductNumber',
                        align: 'center',
                    },
                    {
                        title: '金额',
                        key: 'PlanMoney',
                        align: 'center',
                    },
                    {
                        title: '剩余数量',
                        key: 'RestNumber',
                        align: 'center',
                    },
                    {
                        title: '创建时间',
                        key: 'CreateTimeTemp',
                        align: 'center',
                    },
                ];
                this.numberDis=false;
                var jsondata = {};
                jsondata["Store"] = this.Store;

                var c1Total = 0;
                var c1Current = vueThis.c1Current;
                var c1PageSize = vueThis.c1PageSize;
                jsondata.c1Current = c1Current;
                jsondata.c1PageSize = c1PageSize;
                var data_json = JSON.stringify(jsondata);
                $.ajax({
                    url: "./goPlan/queryAllPlan", //请求的url地址
                    xhrFields: {
                        withCredentials: true
                    },
                    dataType: "json", //返回格式为json
                    data: data_json, //参数值
                    type: "POST", //请求方式
                    headers : {
                        'Content-Type' : 'application/json'
                    },
                    beforeSend: function() {
                        //请求前的处理
                    },
                    success: function(data) {
                        vueThis.data3=data.ResultList;
                        c1Total = data.c1Total;
                        vueThis.c1Total = c1Total;
                    },
                    complete: function() {
                        //请求完成的处理
                    },
                    error: function(data) {

                    }
                });
            }else if (Number(value)==1){

                this.columns3=[
                    {
                        type: 'index',
                        title: '序号',
                        width:46,
                        align: 'center',
                    },
                    {
                        title: '出库编号',
                        key: 'OutNo',
                        align: 'center',
                    },
                    {
                        title: '产品编号',
                        key: 'ProductNo',
                        align: 'center',
                    },
                    {
                        title: '产品名称',
                        key: 'ProductName',
                        align: 'center',
                    },
                    {
                        title: '产品数量',
                        key: 'OutNumber',
                        align: 'center',
                    },
                ];
                this.numberDis=true;
                var jsondata = {};
                jsondata["OutUser"] = this.Store;
                jsondata["DocType"] = 3;
                var c1Total = 0;
                var c1Current = vueThis.c1Current;
                var c1PageSize = vueThis.c1PageSize;
                jsondata.ckCurrent = c1Current;
                jsondata.ckPageSize = c1PageSize;
                var data_json = JSON.stringify(jsondata);
                var vueThis = this;
                $.ajax({
                    url: "./goOutBound/queryAllOutBound", //请求的url地址
                    xhrFields: {
                        withCredentials: true
                    },
                    dataType: "json", //返回格式为json
                    data: data_json, //参数值
                    type: "POST", //请求方式
                    headers : {
                        'Content-Type' : 'application/json'
                    },
                    beforeSend: function() {
                        //请求前的处理
                    },
                    success: function(data) {
                        vueThis.data3=data.ResultList;
                        c1Total = data.ckTotal;
                        vueThis.c1Total = c1Total;
                    },
                    complete: function() {
                        //请求完成的处理
                    },
                    error: function(data) {

                    }
                });
            }
        },

        //双击弹框内容
        clickPlan(value,index){
            console.log(value);
            if (this.inType==0){
                this.RestNumber = value.RestNumber;
                this.formItem.PlanNo=value.PlanNo;
            }
            if (this.inType==1){
                this.RestNumber = value.OutNumber;
                this.formItem.ProductNumber=value.OutNumber;
                this.formItem.PlanNo=value.OutNo;
                this.formItem.FromStore=value.Store;
            }
            this.formItem.DealerNo=value.DealerNo;
            this.formItem.ProductNo=value.ProductNo;
            this.formItem.ProductName=value.ProductName;
            this.formItem.ProductPrice=value.ProductPrice;
            this.plan = false;
        },

        //点击产品
        clickProduck(){
            this.plan = true;
        },

        //鼠标离开数量事件
        numberBlur(){
            if (Number(this.formItem.ProductNumber)>0&&Number(this.formItem.ProductNumber)>Number(this.RestNumber)){
                toastr.error('数量不能大于剩余数量');
                this.formItem.ProductNumber=1;
            }
        },

        //保存
        saveIn(){
            this.formItem.Store = this.Store;
            this.formItem.CreateUser = this.CreateUser;
            if (Number(this.formItem.ProductNumber)>0&&this.formItem.WarehouseNo){
                var data_json = JSON.stringify(this.formItem);
                var vueThis = this;
                $.ajax({
                    url: "./goInBound/saveInBound", //请求的url地址
                    xhrFields: {
                        withCredentials: true
                    },
                    dataType: "json", //返回格式为json
                    data: data_json, //参数值
                    type: "POST", //请求方式
                    headers : {
                        'Content-Type' : 'application/json'
                    },
                    beforeSend: function() {
                        //请求前的处理
                    },
                    success: function(data) {
                        if (data.code==1){
                            toastr.success(data.msg);
                            vueThis.queryIn();
                        }else {
                            toastr.error(data.msg);
                        }
                    },
                    complete: function() {
                        //请求完成的处理
                    },
                    error: function(data) {

                    }
                });
            }else {

            }
        },
        //删除
        deleteIn(InNo,ProductNumber,CreateUser,CreateTime,PlanNo,Type){
            if (InNo&&ProductNumber&&CreateUser&&CreateTime&&PlanNo,Type){
                if (CreateTime!=this.NowDate){
                    toastr.warning("只允许操作当日单据");
                    return false;
                }
                if (CreateUser!=this.CreateUser){
                    toastr.warning("只允许操作自己单据");
                    return false;
                }
                var vueThie = this;
                this.$Modal.confirm({
                    title: '提示',
                    content: '<p>是否删除</p>',
                    onOk: () => {
                        var pa = {};
                        pa["InNo"]=InNo;
                        pa["CreateUser"]=vueThie.CreateUser;
                        pa["ProductNumber"]=ProductNumber;
                        pa["PlanNo"]=PlanNo;
                        pa["Type"]=Type;
                        var data_json = JSON.stringify(pa);
                        $.ajax({
                            url: "./goInBound/delInBound", //请求的url地址
                            xhrFields: {
                                withCredentials: true
                            },
                            dataType: "json", //返回格式为json
                            data: data_json, //参数值
                            type: "POST", //请求方式
                            headers : {
                                'Content-Type' : 'application/json'
                            },
                            beforeSend: function() {
                                //请求前的处理
                            },
                            success: function(data) {
                                if (data.code==1){
                                    toastr.success(data.msg);
                                    vueThie.queryIn();
                                }else {
                                    toastr.error(data.msg);
                                }
                            },
                            complete: function() {
                                //请求完成的处理
                            },
                            error: function(data) {

                            }
                        });
                    },
                    onCancel: () => {
                        toastr.info("取消删除");
                    }
                });
            }else {
                toastr.error("服务器异常，请刷新重试");
            }
        },

        initThisPage(){
            var self = this;
            $.ajax({
                url: "./goLogin/initIndexPage", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                async:false,
                dataType: "json", //返回格式为json
                type: "GET", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    console.log(data);
                    if (data.Bm.indexOf("财务")>=0){
                        self.isBm = true;
                    }else {
                        self.isBm = false;
                    }
                    self.CreateUser=data.UserNo;
                    self.Store = data.Store;
                    self.NowDate=data.nowdate;
                    if(data.Status==0){
                        // top.location.href = "../../login.html";
                        window.location.href="../../login.html";
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });
        },

        initAlert(){
            this.formItem.InNo='';
            this.formItem.Type='';
            this.formItem.ProductNumber=1;
            this.formItem.WarehouseNo='';
            this.formItem.Store='';
            this.formItem.DealerNo='';
            this.formItem.ProductNo='';
            this.formItem.ProductName='';
            this.formItem.ProductPrice='';
            this.formItem.PlanNo='';
            this.formItem.FromStore='';
        }

    },

    created(){
        this.initThisPage();
        this.getWh();
        this.getDealer();
    }

});